<?php

class ControllerImgUpload {

    private $directory;
    private $file;
    private $inputName = "imgFile";
    private $extensions = array('.png', '.gif', '.jpg', '.jpeg');

    function upload($inputName) {
        if (isset($_FILES[$inputName])) {
            if (empty($this->file)) {
                $this->file = basename($_FILES[$inputName]['name']);
            }
            if (move_uploaded_file($_FILES[$inputName]['tmp_name'], $this->directory . $this->file)) {
          } else {
                echo $_FILES[$inputName]['tmp_name'];
            }
        }
    }

    function setDirectory($directory) {
        $this->directory = $directory;
    }

    /**
     * Ajoute l'extension au tableau des extensions pour les images
     * @param type $ext le type exemple .jpg .png
     */
    function setExtension($ext) {
        $this->extensions[sizeof($this->extensions)] = $ext;
    }

    /**
     * return true si le fichier en upload est bien du bon format
     */
    function checkExtension() {
        $extension = strrchr($_FILES[$this->inputName]['name'], '.');
        if (!in_array($extension, $this->extensions)) { //Si l'extension n'est pas dans le tableau
            return false;
        }
        return true;
    }
    
    function getPath(){
        return $this->directory.$this->file;
    }

}

?>